package org.example.gui.widget;

import org.example.dao.BudgetDAO;
import org.example.entity.Budget;
import org.example.gui.model.BudgetTableModel;
import org.example.gui.panel.WorkingPanel;

import javax.swing.*;
import java.awt.*;


public class BudgetTablePanel extends WorkingPanel {
    public static BudgetTablePanel instance = new BudgetTablePanel();
    private final BudgetTableModel model = new BudgetTableModel();
    private final JTable table = new JTable(model);

    private BudgetTablePanel() {
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane sp = new JScrollPane(table);

        this.setLayout(new BorderLayout());
        this.add(sp, BorderLayout.CENTER);

    }

    @Override
    public void updateData() {
        model.budgetList = BudgetDAO.listAllBudget();


        int totalValue = 0;
        double totalSpend = 0;
        for (Budget budget : model.budgetList) {
            totalValue += Integer.parseInt(budget.getValue());
            totalSpend += budget.getRecordTotalSpend();
        }
        Budget yearBudget = new Budget();
        yearBudget.setValue(String.valueOf(totalValue));
        yearBudget.setRecordTotalSpend(totalSpend);
        model.budgetList.add(yearBudget);

        table.updateUI();
    }

    @Override
    public void addListener() {

    }
}
